﻿using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.WebApi.DataEntify;
using Microsoft.Office.Interop.Excel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using LanDBom.Controllers;

namespace LanDBom.Models.Price_sheet
{
    public class Find
    {
        LDbaseEntities LD = new LDbaseEntities();
        public List<Applications> Find_APP(String App_bian)
        {
            List<Applications> listapp = LD.Applications.Where(x => x.AP_bian == App_bian).ToList();
            return listapp;
        }
        public List<BOMini> Find_BOM_Lv0(String App_bian)
        {
            List<BOMini> listbom = LD.BOMini.Where(x => x.BOM2_bian == App_bian && x.BOM2_chen == "Leve0").ToList();
            List<BOMini> listBOM2 = listbom.GroupBy(d => new { d.BOM2_liao }).Select(a => a.OrderByDescending(b => b.BOM2_Zhuang).ElementAt(0)).ToList();
            return listBOM2;
        }
        public List<Ndardo> Find_Nda()
        {
            List<Ndardo> listnda = LD.Ndardo.ToList();
            return listnda;
        }

        public string Find_KH_App(string App_bian)
        {
            if (App_bian != "")
            {
                Applications listapp = LD.Applications.SingleOrDefault(x => x.AP_bian == App_bian);
                return listapp.App_Kehu;
            }
            else
            {
                return "";
            }
        }
        public List<Clirntele> Find_KH(string App_bain)
        {
            string KHname = Find_KH_App(App_bain);

            List<Clirntele> clirnteles = LD.Clirntele.Where(x => x.CL_name == KHname).ToList();
            //
            //集成查询客户
            var client = new K3CloudApi();
            //用于记录结果
            StringBuilder Info = new StringBuilder();
            //业务对象标识
            string formId = "BD_Customer";
            string fieldKeys = "FName,FADDRESS,FEMail,FNumber";//客户 编码 联系电话 邮箱 通讯地址 备注 省份
            string Filter = "FName like'%" + KHname + "%'";
            //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式
            var param = new QueryParam()
            {
                FormId = formId,
                FieldKeys = fieldKeys,
                FilterString = Filter,
                TopRowCount = 0,
                StartRow = 0,
                Limit = 20,
            };
            //调用接口
            var result = client.ExecuteBillQuery(param.ToJson());


            //返回的数据类型为List<List<object>>，错误的返回结果只有一行数据且IsSuccess为False
            if (result.Count == 1)
            {
                //对返回结果进行解析和校验，这里使用的是JsonPatch
                var resultJObject = JArray.Parse(JsonConvert.SerializeObject(result[0]));
                var queryNode = resultJObject.SelectToken("$..IsSuccess");
                if (queryNode != null)
                {
                    var isSuccess = queryNode.Value<Boolean>();
                    Info.AppendLine(isSuccess ? "操作成功" : "操作失败");
                }
                else
                {
                    Jichengss(clirnteles, result);
                    Info.AppendLine("操作成功");
                }
            }
            else
            {
                Jichengss(clirnteles, result);
                Info.AppendLine("操作成功");
            }

            return clirnteles;
        }
        public List<Applications> Find_APP(string APP_bian, string App_KHname, string App_xiang,string Fzr)
        {
            try
            {
                List<Applications> listapp = LD.Applications.Where(x => x.App_bu == "7").OrderByDescending(s=>s.App_qishi).ToList();
                foreach (var app in listapp)
                {
                    if (app.AP_bian == null)
                    {
                        app.AP_bian = "";
                    }
                    if (app.App_Kehu == null)
                    {
                        app.App_Kehu = "";
                    }
                    if (app.App_qishi == null)
                    {
                        app.App_qishi = "";
                    }
                    if (app.App_BanB == null)
                    {
                        app.App_BanB = "1";
                    }
                    if (app.App_fzr == null)
                    {
                        app.App_fzr = "";
                    }
                }
                if (APP_bian != "")
                {
                    listapp = listapp.Where(x => x.AP_bian.Contains(APP_bian)).ToList();
                }
                if (App_KHname != "" && App_KHname != null)
                {
                    listapp = listapp.Where(a => a.App_Kehu.Contains(App_KHname)).ToList();
                }
                if (App_xiang != "" && App_xiang != null)
                {
                    listapp = listapp.Where(x => x.App_qishi.Contains(App_xiang)).ToList();
                }
                if (Fzr != null && Fzr != "")
                {
                    listapp = listapp.Where(x => x.App_fzr.Contains(Fzr)).ToList();
                }
                listapp = listapp.GroupBy(d => new { d.AP_bian }).Select(a => a.OrderByDescending(b => b.App_BanB).ElementAt(0)).ToList();
                return listapp;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static void Jichengss(List<Clirntele> cli, List<List<object>> result)
        {
            for (int i = 0; i < result.Count; i++)
            {
                var ghjk = new Clirntele()
                {
                    CL_name = (string)result[i][0],
                    CL_sho = (string)result[i][1],
                    CL_you = (string)result[i][2],
                    CL_bian = (string)result[i][3],
                };
                cli.Add(ghjk);
            }
        }
    }
}
